/*
 * Galaxium Messenger
 * Copyright (C) 2007 Ben Motmans <ben.motmans@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using Galaxium.Core;

using Anculus.Core;

namespace Galaxium.Protocol.Aim
{
	public sealed class AimAccountCache : AbstractAccountCache
	{
		public AimAccountCache (IAccount account, string directory) : base (account, directory)
		{
			
		}
		
		public override void SaveAccountImage (string filename)
		{
			
		}
		
		public override void SaveAccountImage ()
		{
			string hash = BaseUtility.GetHashedName (Account.UniqueIdentifier);
			string contextFile = Path.Combine(Directory, hash + ".context");
			string cacheFile = Path.Combine (Directory, hash + ".cache");
			
			TextWriter contextWriter = new StreamWriter(File.Create(contextFile));
			FileStream fileStream = File.Create(cacheFile);
			
			if(fileStream != null && contextWriter != null)
			{
				fileStream.Write(Account.DisplayImage.ImageBuffer, 0, Account.DisplayImage.ImageBuffer.Length);
				fileStream.Close();
				
				//((AimDisplayImage)Account.DisplayImage).Data = File.ReadAllBytes(cacheFile);
				
				//Log.Info("Saving Account Image {0}", ((MsnDisplayImage)Account.DisplayImage).Context);
				
				//contextWriter.WriteLine(((AimDisplayImage)Account.DisplayImage).Context);
				contextWriter.Close();
			}
		}
		
		public override void GetAccountImage ()
		{
			string hash = BaseUtility.GetHashedName (Account.UniqueIdentifier);
			string contextFile = Path.Combine(Directory, hash + ".context");
			string cacheFile = Path.Combine (Directory, hash + ".cache");
			
			if (File.Exists(cacheFile) && File.Exists(contextFile))
			{
				AimDisplayImage image = new AimDisplayImage(Account.Session);
				
				TextReader contextReader = new StreamReader(File.Open(contextFile, FileMode.Open));
				
				//image.Context = contextReader.ReadLine();
				
				//Log.Info("Loading Cached Account Image {0}", image.Context);
				
				//image.Data = File.ReadAllBytes(cacheFile);
				
				//Log.Info("Set Data {0}", image.Context);
				
				contextReader.Close();
				
				Account.DisplayImage = image;
			}
		}
		
		public override void SaveDisplayImage (IContact contact)
		{
			string hash = BaseUtility.GetHashedName (contact.UniqueIdentifier);
			
			string contextFile = Path.Combine(ContactsDirectory, hash + ".context");
			string cacheFile = Path.Combine (ContactsDirectory, hash + ".cache");
			
			TextWriter contextWriter = new StreamWriter(File.Create(contextFile));
			FileStream fileStream = File.Create(cacheFile);
			
			if (fileStream != null && contextWriter != null)
			{
				fileStream.Write(contact.DisplayImage.ImageBuffer, 0, contact.DisplayImage.ImageBuffer.Length);
				fileStream.Close();
				
				//((AimDisplayImage)contact.DisplayImage).Data = File.ReadAllBytes(cacheFile);
				
				//Log.Info("Saving Display Image {0}", ((MsnDisplayImage)contact.DisplayImage).Context);
				
				//contextWriter.WriteLine(((AimDisplayImage)contact.DisplayImage).Context);
				contextWriter.Close();
			}
		}
		
		public override IDisplayImage GetDisplayImage (IContact contact)
		{
			string hash = BaseUtility.GetHashedName (contact.UniqueIdentifier);

			string contextFile = Path.Combine(ContactsDirectory, hash + ".context");
			string cacheFile = Path.Combine (ContactsDirectory, hash + ".cache");
			
			if (File.Exists(contextFile) && File.Exists(cacheFile))
			{
				AimDisplayImage image = new AimDisplayImage(Account.Session);
				
				TextReader contextReader = new StreamReader(File.Open(contextFile, FileMode.Open));
				//image.Context = contextReader.ReadLine();
				contextReader.Close();
				
				//Log.Info("Loading Cached Display Image {0}", image.Context);
				
				//image.Data = File.ReadAllBytes(cacheFile);
				
				//Log.Info("Set Data {0}", image.Context);
				
				return image;
			}
			
			return null;
		}
	}
}